package com.henry.service.impl;


import com.henry.service.SmsService;
import com.henry.utils.HttpUtils;
import com.henry.utils.RandomUtil;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description
 * @author Henry Guan
 * @since 2023-08-19
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean sendCode(String phone) {
        //测试开发时，所以判断redis是否存了验证码，就不发送sms了
        String code = (String) redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)) {
            System.out.println(code);
            return true;
        }

        String host = "https://mysms.market.alicloudapi.com";
        String path = "/sms";
        String method = "POST";
        String appcode = "69ece3ddbbd44ac3bf2eca9ffdbedcbb";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        String fourBitRandom = RandomUtil.getFourBitRandom();
        System.out.println(fourBitRandom);
        querys.put("content", fourBitRandom);
        querys.put("mobile", phone);
        String bodys = "";

        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));

            //把验证码保存到redis中
            redisTemplate.opsForValue().set(phone,fourBitRandom,500, TimeUnit.DAYS);//设置过期时间
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
